package com.mz.mattress.api.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.EcommerceService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpOAuth2ServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxConfig {

    @Autowired
    private WxProperties wxProperties;

    @Bean
    public WxMaService clientWxMaService() {
        WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
        wxMaDefaultConfig.setAppid(wxProperties.getClientAppId());
        wxMaDefaultConfig.setSecret(wxProperties.getClientAppSecret());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(wxMaDefaultConfig);
        return service;
    }

    @Bean
    public WxMaService merchantWxMaService() {
        WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
        wxMaDefaultConfig.setAppid(wxProperties.getMerchantAppId());
        wxMaDefaultConfig.setSecret(wxProperties.getMerchantAppSecret());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(wxMaDefaultConfig);
        return service;
    }

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        WxMpDefaultConfigImpl wxConfigProvider = new WxMpDefaultConfigImpl();
        WxMpHostConfig hostConfig = new WxMpHostConfig();
        hostConfig.setMpHost("127.0.0.1");
        wxConfigProvider.setHostConfig(hostConfig);
        wxConfigProvider.setAppId(wxProperties.getMpAppId());
        wxConfigProvider.setSecret(wxProperties.getMpAppSecret());
        wxMpService.setWxMpConfigStorage(wxConfigProvider);
        return wxMpService;
    }

    @Bean
    public WxOAuth2Service wxOAuth2Service(WxMpService wxMpService) {
        WxOAuth2Service service = new WxMpOAuth2ServiceImpl(wxMpService);
        return service;
    }

    @Bean
    public WxPayConfig wxPayConfig() {

//        WxPayConfig wxPayConfig = new WxPayConfig();
//        wxPayConfig.setMchId(wxProperties.getSpMchId());
//        wxPayConfig.setPrivateKeyPath(wxProperties.getPrivateKey());
//        wxPayConfig.setPrivateCertPath(wxProperties.getPrivateCertPem());
//        wxPayConfig.setApiV3Key(wxProperties.getApiV3Key());

        WxPayConfig payConfig = new WxPayConfig();

        payConfig.setPrivateKeyPath(wxProperties.getPrivateKey());
        payConfig.setPrivateCertPath(wxProperties.getPrivateCertPem());

        payConfig.setAppId(wxProperties.getClientAppId());
        payConfig.setMchId(wxProperties.getMerId());
        payConfig.setApiV3Key(wxProperties.getApiV3Key());
        payConfig.setMchKey(wxProperties.getMerSecret());
        payConfig.setKeyPath(wxProperties.getKeyPath());
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig wxPayConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }

//    @Bean
//    public WxPayService wxPayService() {
//
//        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setAppId(wxProperties.getClientAppId());
//        payConfig.setMchId(wxProperties.getSpMchId());
//        payConfig.setMchKey(wxProperties.getApiV3Key());
//        payConfig.setKeyPath(wxProperties.getKeyPath());
//
//        payConfig.setUseSandboxEnv(false);
//        WxPayService wxPayService = new WxPayServiceImpl();
//        wxPayService.setConfig(payConfig);
//        return wxPayService;
//    }

    /**
     * 获取收付通service
     */
    @Bean
    public EcommerceService getEcommerceService(WxPayService wxPayService) {
        return wxPayService.getEcommerceService();
    }

}
